package com.lkm.asking.service.Impl;

import com.lkm.asking.dao.AdminDao;
import com.lkm.asking.dao.DataDao;
import com.lkm.asking.entity.Admin;
import com.lkm.asking.entity.Data;
import com.lkm.asking.service.AdminService;
import com.lkm.asking.service.DataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
//import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;



@Service
public class DataServiceImpl implements DataService {
    @Autowired
    DataDao dataDao;

    @Value("${IP1}")
    String ip;


    @Override
    public int insert(Data data) {
//        try {
////            data.getMultipartFile().transferTo(new File("D:\\" + data.getName()));
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        //文件保存*
        String pathe;
        String imgs = data.getImg();
        String username = data.getUserName();
        String path="E:/data";
        MultipartFile file = data.getMultipartFile();   //得到文件
        String fileName = file.getOriginalFilename();   //获得文件的名称


        File dest = new File(new File(path).getAbsolutePath()+"/"+username+"/"+imgs+"/"+fileName);   //文件路径

        path=new File(path).getAbsolutePath()+"/"+username+"/"+imgs+"/";
        //String[] paths = path.split("\\\\");
        String[] paths = path.split("/");
        StringBuffer fullPath = new StringBuffer();
        for (int i = 0; i < paths.length; i++) {
            //fullPath.append(paths[i]).append("\\");
            fullPath.append(paths[i]).append("/");
            File tempDir = new File(fullPath.toString());
            if (!tempDir.exists()) {
                tempDir.mkdir();
            }
        }
//        File dest=new File("D:/data/"+fileName);
        //确保父目录存在
//        if (!dest.getParentFile().exists()){
//            dest.getParentFile().mkdir();
//        }

        try{
            file.transferTo(dest);   //将文件传到指定的目录中
        }catch (Exception e){
            e.printStackTrace();
        }
        //文件保存*
        String suffix = fileName.substring((fileName.lastIndexOf(".")));
        String sort = fileName.substring((fileName.lastIndexOf(".")+1));
        String newname = ip + "/" + sort+ "/"+username+"/"+imgs+ "/"+ fileName;

        data.setImg(newname);

//        data.getImg()
        return dataDao.insert(data);
    }


    @Override
    public List<Data> listData(String userName) {
        return dataDao.listData(userName);
    }

    public int delData(int id) {
        return  dataDao.delData(id);
    }
    public List<Data> listDataByKind(String bookmark) {
        String[] split = bookmark.split("\\+");
        Set<Data> result = null;
        List<Set<Data>> lists = new ArrayList<>();
        for(String item:split){
            Set<Data> collect = dataDao.listDataByKind("%" + item + "%").stream().collect(Collectors.toSet());
            lists.add(collect);
            if(result ==null && collect !=null && collect.size()!=0){
                result = collect;
            }
        }
        if(lists.size()==0){
            return new ArrayList<>();
        }
        result.forEach(item->{
            System.out.print(item+" ");
        });
        for(Set<Data> item:lists){
            result.retainAll(item);
        }
        System.out.println();
        for (Data data : result) {
            System.out.print(data);
        }
        return result.stream().collect(Collectors.toList());
    }

    public int addOne(int id) {
        Integer results;
         results=dataDao.addOne(id);
         if(results==null){
             return 0;
         }
         return results;
    }

    public int addRead(int id) {
        Integer results;
        results=dataDao.addRead(id);
        if(results==null){
            return 0;
        }
        return results;
    }
    public List<Data> listDataBySchool(String school) {
        return dataDao.listDataBySchool(school);

    }

    public List<Data> listDataByID(Integer id) {
//        dataDao.addRead()
        return dataDao.listDataByID(id);
    }

    @Override
    public Integer decOne(Integer id) {
        return dataDao.decOne(id);
    }

    public Data getOne(Integer id){
        return dataDao.getOne(id);
    }

    @Override
    public List<Data> listByName(String username) {
        return dataDao.listData(username);
    }

}
 